<template>
  <page-header-wrapper :title="false">
    <a-card title="业务说明">
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务分类">
                <a-select
                  show-search
                  v-model="queryParam['filter[category_id]']"
                  placeholder="请选择业务分类"
                  :filter-option="filterOption"
                  @change="onChange"
                >
                  <a-select-option v-for="(item,index) in categoryOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="状态名称">
                <a-select
                  show-search
                  v-model="queryParam['filter[state_name]']"
                  placeholder="请选择业务状态名称"
                  :filter-option="filterOption"
                >
                  <a-select-option v-for="(item,index) in stateOpts" :key="index" :value="item">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="notesColumns"
          :data="notesData"
        >
          <template slot="category" slot-scope="text">
            {{categoryOpts[text]}}
          </template>
          <template slot="filename" slot-scope="text,record">
            <a-button type="link" size="small" @click="openPdf(record.file_url)">{{text}}</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { STable } from '@/components'
  import otherColumns from '@/columns/other'
  import { getBusinessExplain, getExplainState } from '@/api/business'
  export default {
    components: {
      STable
    },
    name: 'BusinessNotes',
    data () {
      return {
        advanced: true,
        queryParam: {},
        notesColumns: otherColumns.notes,
        notesData: parameters => {
          return getBusinessExplain(Object.assign(parameters, this.queryParam)).then(res => {
            this.categoryOpts = res.result.category_opts
            return res.result
          })
        },
        stateOpts: {},
        categoryOpts: {},
        tab: '10'
      }
    },
    methods: {
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      },
      openPdf (url) {
        window.open(url)
      },
      onChange (key) {
        const app = this
        getExplainState(key).then(res => {
          if (res.status === 0) {
            app.stateOpts = res.result.stateOpts
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
